export const emojiList = [
  {
    emoji: "😄",
    rule: "[AA]",
  },
  {
    emoji: "😒",
    rule: "[B]",
  },
  {
    emoji: "😭",
    rule: "[C]",
  },
  {
    emoji: "😎",
    rule: "[D]",
  },
  {
    emoji: "🙂",
    rule: "[E]",
  },
];

export function addEmoji(emoji: string, rule: string) {
  emojiList.push({
    emoji,
    rule,
  });
}

// 关键正则替换
export function emojiReplaceString(str: string) {
  // 将所有表情按照|拼接成字符串
  const emoji = emojiList.map((v) => v.emoji).join("|");
  // 转化为正则表达式
  const reg = new RegExp(emoji, "g");
  // 替换所有匹配成功的
  return str.replace(reg, ($) => {
    // 查找表情对象的对一个项目
    const item = emojiList.find((v) => v.emoji === $);
    // 替换
    return item?.rule || $;
  });
}

export function stringReplaceEmoji(str: string) {
  const reg = /\[[A-Z]+\]/g;

  return str.replace(reg, ($) => {
    const item = emojiList.find((v) => v.rule === $);
    return item?.emoji || $;
  });
}
